home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #1 / Amiga Plus 1995 #1.iso / animationen / retina_fli_ii / read.c < prev    next >
C/C++ Source or Header  |  1994-12-13  |  14KB  |  558 lines

  1. /*
  2.                xflick - Ron Schnell, March, 1991
  3.  
  4.            This code is provided as is, with no warrantees, expressed
  5.                or implied.  I believe this code to be free of encumbrance,
  6.            and offer it to the public domain.  I ask, however, that
  7.            this paragraph and my name be retained in any modified
  8.            versions of the file you may make, and that you notify me
  9.            of any improvements you make to the code.
  10.  
  11.            Ron Schnell (ronnie@sos.com)
  12. */
  13.  
  14. /*
  15.     The following changes are from Michael Pall
  16.     (pall@rz.uni-karlsruhe.de) Mar 25-28 1991:
  17.  
  18.     Lots of bugfixes and changes to the structure of the files.
  19.     The interpretation part is now in this file.
  20. */
  21.  
  22. /*    This is read.c, the functions which read in FLI structures in 
  23.       a machine independent format.  */
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. /* #include <unistd.h> */
  29. #include "xflick.h"
  30.  
  31. #define TRUE 1
  32. #define FALSE 0
  33.  
  34. #define DEBUGGING 0
  35.  
  36. extern int verbose;
  37.  
  38. void read_flihead(int fd, struct fli_header *header)
  39. {
  40.     unsigned char *buf = (unsigned char *)malloc(SIZE_HEADER);
  41.     unsigned char *sbf;
  42.     int i;
  43.  
  44.     sbf = buf;
  45.     (void) read(fd, buf, SIZE_HEADER);
  46.     header->fhd_size = get_long(buf);
  47.     buf += LONGSIZE;
  48.  
  49.     header->fhd_magic = get_short(buf);
  50.     buf += SHORTSIZE;
  51.  
  52.     header->fhd_frames = get_short(buf);
  53.     buf += SHORTSIZE;
  54.  
  55.     header->fhd_width = get_short(buf);
  56.     buf += SHORTSIZE;
  57.  
  58.     header->fhd_height = get_short(buf);
  59.     buf += SHORTSIZE;
  60.  
  61.     header->fhd_gap = get_short(buf);
  62.     buf += SHORTSIZE;   /* nothing */
  63.  
  64.     header->fhd_flags = get_short(buf);
  65.     buf += SHORTSIZE;   /* flags */
  66.  
  67.     header->fhd_speed = get_short(buf);
  68.     buf += SHORTSIZE;
  69.  
  70.     header->fhd_next = get_long(buf);
  71.     buf += LONGSIZE;
  72.  
  73.     header->fhd_frit = get_long(buf);
  74.     buf += LONGSIZE;
  75.  
  76.     bcopy(buf, header->fhd_expand, FHD_EXPAND_SIZE);
  77.  
  78.     free(sbf);
  79.  
  80.     if (verbose) {
  81.       fprintf(stderr, "header->fhd_size: %d\n", header->fhd_size);
  82.       fprintf(stderr, "header->fhd_magic: 0x%x\n", header->fhd_magic);
  83.       fprintf(stderr, "header->fhd_frames: %d\n", header->fhd_frames);
  84.       fprintf(stderr, "header->fhd_width: %d\n", header->fhd_width);
  85.       fprintf(stderr, "header->fhd_height: %d\n", header->fhd_height);
  86.       fprintf(stderr, "header->fhd_gap: 0x%x\n", header->fhd_gap);
  87.       fprintf(stderr, "header->fhd_flags: 0x%x\n", header->fhd_flags);
  88.       fprintf(stderr, "header->fhd_speed: %d\n", header->fhd_speed);
  89.       fprintf(stderr, "header->fhd_next: %d\n", header->fhd_next);
  90.       fprintf(stderr, "header->fhd_frit: 0x%x\n", header->fhd_frit);
  91.       fprintf(stderr, "header->fhd_expand: ");
  92.       for (i = 0; i < FHD_EXPAND_SIZE; i++)
  93.     fprintf(stderr, "%x ", 0xff & (unsigned int)header->fhd_expand[i]);
  94.       fprintf(stderr, "\n");
  95.     }
  96. }
  97.  
  98. void read_fheader(int fd, struct frame_header *fheader)
  99. {
  100.     unsigned char data[SIZE_FHEADER];
  101.     unsigned char *buf = data;
  102.     int ret;
  103.     unsigned char tmp;
  104.     int syncNotFound = TRUE;
  105.     int i;
  106.  
  107.     /* Syncronize to the magic word */
  108.     i = 0;
  109.     do {
  110.       ret = read(fd, &tmp, 1);
  111.       ++i;
  112.       if (verbose)
  113.     fprintf(stderr, "read1 i: %d tmp: 0x%x sync: %d\n",
  114.         i, 0xff & (unsigned int)tmp, syncNotFound);
  115.       if (tmp == (unsigned char)0xfa) {
  116.     ret = read(fd, &tmp, 1);
  117.     ++i;
  118.     if (tmp == (unsigned char)0xf1)
  119.       syncNotFound = FALSE;
  120.     else
  121.       syncNotFound = TRUE;
  122.     if (verbose)
  123.       fprintf(stderr, "read2 i: %d tmp: 0x%x sync: %d\n",
  124.           i, 0xff & (unsigned int)tmp, syncNotFound);
  125.       }
  126.     } while((syncNotFound == TRUE) && (ret > 0));
  127.  
  128.     if (ret < 0) {
  129.       fprintf(stderr,
  130. "Error reading input file looking for frame header magic number. i: %d\n", i);
  131.       exit(-1);
  132.     }
  133.     
  134.     if (verbose)
  135.       fprintf(stderr, "After Sync i: %d sync: %d ret: %d\n",
  136.           i, syncNotFound, ret);
  137.  
  138.     if ((ret = lseek(fd, -6, SEEK_CUR)) < 0) {
  139.       perror("Seeking to begining of frame");
  140.       exit(-1);
  141.     }
  142.  
  143.     (void) read(fd, buf, SIZE_FHEADER);
  144.  
  145.     fheader->fr_size = get_long(buf);
  146.     buf += LONGSIZE;
  147.     fheader->fr_magic = get_short(buf);
  148.     buf += SHORTSIZE;
  149.     fheader->fr_chunks = get_short(buf);
  150.     bcopy(buf, fheader->fr_expand, FR_EXPAND_SIZE);
  151.  
  152.  
  153.     if (verbose) {
  154.       fprintf(stderr, "fheader->fr_size: %d\n", fheader->fr_size);
  155.       fprintf(stderr, "fheader->fr_magic: 0x%x\n", fheader->fr_magic);
  156.       fprintf(stderr, "fheader->fr_chunks: %d\n", fheader->fr_chunks);
  157.       fprintf(stderr, "fheader->fr_expand: ");
  158.       for (i = 0; i < FR_EXPAND_SIZE; i++)
  159.     fprintf(stderr, "%d ", 0xff & (unsigned int)fheader->fr_expand[i]);
  160.       fprintf(stderr, "\n");
  161.     }
  162.  
  163. }
  164.  
  165. void read_chunk(int fd, struct chunk_header *chunk)
  166. {
  167.     unsigned char data[SIZE_CHEADER];
  168.     unsigned char *buf = data;
  169.  
  170.     (void) read(fd, buf, SIZE_CHEADER);
  171.     chunk->ch_bytes = get_long(buf);
  172.     buf += LONGSIZE;
  173.     chunk->ch_type = get_short(buf);
  174.  
  175.     if (verbose) {
  176.       fprintf(stderr, "chunk->ch_bytes: %d\n", chunk->ch_bytes);
  177.       fprintf(stderr, "chunk->ch_type: %d\n", chunk->ch_type);
  178.     }
  179. }
  180.  
  181. void interpret_fli(int fd, struct fli_header *header,
  182.            unsigned char *data, int notfirst, void (*func)())
  183. /* int fd; */            /* The file descriptor */
  184. /* struct fli_header *header; */    /* The header of the file */
  185. /* unsigned char *data; */    /* A pointer to the pixel data */
  186. /* int notfirst; */        /* Is this not the first time through? */
  187. /* void (*func)(); */        /* The function that stores or displays the chunks */
  188. {
  189.     int i, j, s, l, k;            /* Random look/status ints */
  190.     short ndiff;        /* To hold the # lines changed from last frame */
  191.     struct frame_header fheader;    /* To hold each frame header */
  192.     struct chunk_header chunk;        /* To hold each chunk header */
  193.     unsigned char *buf=(unsigned char *)0;
  194.                     /* malloc'd space to hold each chunk */
  195.     int bufalloc;            /* Space already allocated for buf */
  196.     int skchange, skcount, sicount;  /* Various pixel/data index's */
  197.     unsigned char *cptr,*dptr,*eptr;     /* data pointers into *buf */
  198.     int npack;                /* Number of packets in this chunk */
  199.     unsigned char by, by0, by1;        /* To hold a byte */
  200.     int curx, cury;            /* The current x & y coords */
  201.     short wwidth, wheight;        /* Width and height of the window */
  202.     int topy,bottomy,leftx,rightx;  /* For optimizing FLI_LC chunks */
  203.  
  204.     static long twopos;            /* To keep the file offset of frame # 2 */
  205.  
  206.     wwidth = header->fhd_width;
  207.     wheight = header->fhd_height;
  208.  
  209.     for (i=0; i <= header->fhd_frames; i++)
  210.     {
  211.  
  212.     /* The following stuff ist only needed if we display the frames
  213.        while we interpret the file */
  214.  
  215.     /* If this is the first time through, we display frame # 1,
  216.        otherwise, we skip right to frame two */
  217.  
  218.     if ((notfirst) && (i==0))
  219.     {
  220.         (void) lseek(fd, twopos, 0);
  221.         continue;
  222.     }
  223.  
  224.     /* If this is the first time through and we are at frame #2,
  225.        save our file offset for the next loop through */
  226.         
  227.     if (!(notfirst) && (i==1))
  228.         twopos = lseek(fd, 0, 1);
  229.         
  230.     /* Each frame has a header */
  231.  
  232.     read_fheader(fd, &fheader);
  233.  
  234.     for (j=0; j < fheader.fr_chunks; j++)
  235.     {
  236.         /* Each chunk also has a header */
  237.         read_chunk(fd, &chunk);
  238.  
  239.         /* Malloc the space to hold whole chunk, unless it
  240.            is a COPY chunk. Malloc/Realloc only if necessary */
  241.  
  242.         if (chunk.ch_type != FLI_COPY) {
  243.         if (!buf)
  244.         {
  245.             buf = (unsigned char *)malloc(chunk.ch_bytes);
  246.             bufalloc = chunk.ch_bytes;
  247.         }
  248.         else
  249.         {
  250.             if (bufalloc < chunk.ch_bytes)
  251.             {
  252.             buf = (unsigned char *)realloc(buf, chunk.ch_bytes);
  253.             bufalloc = chunk.ch_bytes;
  254.             }
  255.         }
  256.           }    
  257.  
  258.         /* A COPY chunk means 64k of uncompressed image data,
  259.            read it directly into the image */
  260.  
  261.         if (chunk.ch_type == FLI_COPY)
  262.         s = read(fd, data, wwidth*wheight);
  263.         else
  264.         s = read(fd, buf, chunk.ch_bytes -  6);
  265.  
  266.         if (s < 0) {
  267.         fprintf(stderr, "s: %d fd: %d data: 0x%x chunk.ch_bytes: %d\n",
  268.             s, fd, data, chunk.ch_bytes);
  269.         xferror("Error reading image data.");
  270.           }
  271.  
  272.         cptr = buf;
  273.  
  274.         switch (chunk.ch_type) {
  275.         case FLI_COPY:
  276.         (*func)(FLI_COPY, i, data, 0,0, 0,0, wwidth,wheight);
  277.         break;
  278.  
  279.         /* A BLACK chunk */
  280.  
  281.         case FLI_BLACK:
  282.         bzero(data, wwidth * wheight);
  283.         (*func)(FLI_BLACK, i, data, 0,0, 0,0, wwidth,wheight);
  284.         break;
  285.  
  286.         /* A COLOR chunk:  A compressed colormap to be stored or
  287.            changed */
  288.  
  289.         case FLI_256_COLOR:
  290.         npack = get_short(cptr);
  291.         cptr += SHORTSIZE;
  292.  
  293.         for (l=0; l < npack; l++)
  294.         {
  295.             skcount = *(cptr++);     /* # colors not to change */
  296.             skchange = *(cptr++);    /* # of colors to change */
  297.             if (!skchange)           /* (0 means all) */
  298.             skchange=MAXCOL;
  299.         
  300.             /* The colors are stored in the right 6 bits
  301.                of the byte.  We then convert it to a short. */
  302.  
  303.             (*func)(FLI_256_COLOR, i, cptr, skcount, 
  304.                 skchange, 0,0, 0,0);
  305.             cptr += skchange * 3;
  306.         }
  307.         break;
  308.  
  309.         case FLI_COLOR:
  310.         npack = get_short(cptr);
  311.         cptr += SHORTSIZE;
  312.  
  313.         for (l=0; l < npack; l++)
  314.         {
  315.             skcount = *(cptr++);     /* # colors not to change */
  316.             skchange = *(cptr++);    /* # of colors to change */
  317.             if (!skchange)           /* (0 means all) */
  318.             skchange=MAXCOL;
  319.         
  320.             /* The colors are stored in the right 6 bits
  321.                of the byte.  We then convert it to a short. */
  322.  
  323.             (*func)(FLI_COLOR, i, cptr, skcount, skchange, 0,0, 0,0);
  324.             cptr += skchange * 3;
  325.         }
  326.         break;
  327.  
  328.  
  329.         /* Line compressed, and bytewise/run-length compressed
  330.            chunks are similar.  BRUN chunks always start at the
  331.            top, while LC chunks can skip lines.  Also, LC chunks
  332.                can skip bytes on each line.  The sign of the
  333.            byte also has opposite meanings for the two! */
  334.  
  335.         case FLI_LC:
  336.         curx = 0;
  337.         cury = get_short(cptr);     /* Lines at top to skip */
  338.         cptr += SHORTSIZE;
  339.         ndiff = get_short(cptr);    /* # of lines different */
  340.         cptr += SHORTSIZE;
  341.  
  342.         topy = cury;
  343.         bottomy = cury + ndiff;
  344.  
  345.             leftx = wwidth;
  346.             rightx = 0;
  347.     
  348.         for (k=0; k < ndiff; k++)    /* Go through each line */
  349.         {
  350.             npack = *(cptr++);       /* # packets this line */
  351.  
  352.             for (l=0; l < npack; l++)
  353.             {
  354.             skcount = *(cptr++);    /* # bytes to skip */
  355.             
  356.             if (l == 0 && skcount < leftx)
  357.                 leftx = skcount;
  358.  
  359.             curx += skcount;
  360.  
  361.             /* The number of bytes in this packet to change */
  362.  
  363.             sicount = *(cptr++);
  364.  
  365.             /* Positive, for LC means bytes the changes follow,
  366.                for BRUN means one byte to follow, spread it
  367.                through. */
  368.  
  369.             if (sicount < 128)
  370.             {
  371.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  372.                   sicount);
  373.                 cptr += sicount;
  374.                 curx += sicount;
  375.             }
  376.  
  377.             else
  378.  
  379.             /* Negative, the opposite */
  380.  
  381.             {
  382.                 sicount = 256 - sicount;
  383.                 by = *(cptr++);
  384.                 dptr = data + wwidth * cury + curx;
  385.                 eptr = dptr + sicount;
  386.                 do
  387.                 *(dptr++) = by;
  388.                 while (dptr < eptr);
  389.                 curx += sicount;
  390.             }
  391.             }
  392.  
  393.             if (curx > rightx)
  394.             rightx = curx;
  395.  
  396.             /* Next line */
  397.             cury++;
  398.             curx = 0;
  399.         }
  400.         
  401.         (*func)(FLI_LC, i, data, leftx,topy, leftx,topy,
  402.             rightx-leftx,bottomy-topy);
  403.         break;
  404.  
  405.         case FLI_DELTA:
  406.         curx = 0;
  407.         cury = 0;
  408.  
  409.         by0 = *(cptr++);
  410.         by1 = *(cptr++);
  411.         ndiff = by0 + 256 * by1;     /* # of lines different */
  412.  
  413.         /* printf(" -- -- ndiff: %d  frame: %d\n",ndiff,i); */
  414.  
  415.         topy = 0;
  416.         bottomy = 0;
  417.  
  418.             leftx = wwidth;
  419.             rightx = 0;
  420.     
  421.         k=0;
  422.         while (k < ndiff)            /* Go through each line */
  423.         {
  424.             by0 = *(cptr++);
  425.             by1 = *(cptr++);
  426.             npack = by0 + 256 * by1;
  427.             if (npack >= 32768) npack -= 65536;
  428.  
  429.             /* printf(" -- npack: %d  cury: %d\n",npack,cury); */
  430.  
  431.             if (npack < 0)        /* skip lines */
  432.             {
  433.             if (cury == 0)
  434.             {
  435.                 topy = -npack;
  436.             }
  437.             cury -= npack;
  438.             continue;
  439.             }
  440.  
  441.             bottomy = cury+1;
  442.  
  443.             for (l=0; l < npack; l++)
  444.             {
  445.             skcount = *(cptr++);    /* # bytes to skip */
  446.             
  447.             if (l == 0 && skcount < leftx)
  448.                 leftx = skcount;
  449.  
  450.             curx += skcount;
  451.             /* printf("pack: %d  curx: %d  skip: %d\n",
  452.                     l,curx,skcount); */
  453.  
  454.             /* The number of words in this packet to change */
  455.  
  456.             sicount = *(cptr++);
  457.  
  458.             /* Positive, for DELTA means sicount words (16 bit) 
  459.                that change follow */
  460.             /* printf("sicount: %d\n",sicount); */
  461.  
  462.             if (sicount < 128)
  463.             {
  464.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  465.                   2 * sicount);
  466.                 cptr += 2 * sicount;
  467.                 curx += 2 * sicount;
  468.             }
  469.  
  470.             else
  471.  
  472.             /* Negative, the opposite */
  473.  
  474.             {
  475.                 sicount = 256 - sicount;
  476.                 by0 = *(cptr++);
  477.                 by1 = *(cptr++);
  478.                 dptr = data + wwidth * cury + curx;
  479.                 eptr = dptr + 2 * sicount;
  480.                 do
  481.                 {
  482.                 *(dptr++) = by0;
  483.                 *(dptr++) = by1;
  484.                 }
  485.                 while (dptr < eptr);
  486.                 curx += 2 * sicount;
  487.             }
  488.             }
  489.  
  490.             if (curx > rightx)
  491.             rightx = curx;
  492.  
  493.             /* Next line */
  494.             cury++;
  495.             curx = 0;
  496.             k++;
  497.         }
  498.  
  499.         /* printf(" top: %d  bot: %d    left: %d   right: %d\n",
  500.             topy, bottomy, leftx, rightx); */
  501.         (*func)(FLI_DELTA, i, data, leftx,topy, leftx,topy,
  502.             rightx-leftx,bottomy-topy);
  503.         break;
  504.  
  505.         case FLI_BRUN:
  506.         dptr = data;
  507.     
  508.         for (k=0; k < wheight; k++)    /* Go through each line */
  509.         {
  510.             npack = *(cptr++);       /* # packets this line */
  511.  
  512.             for (l=0; l < npack; l++)
  513.             {
  514.             /* The number of bytes in this packet to change */
  515.  
  516.             sicount = *(cptr++);
  517.  
  518.             /* Positive, for LC means bytes the changes follow,
  519.                for BRUN means one byte to follow, spread it
  520.                through. */
  521.  
  522.             if (sicount < 128)
  523.             {
  524.                 by = *cptr++;
  525.                 eptr = dptr + sicount;
  526.                 while (dptr < eptr)
  527.                 *(dptr++) = by;
  528.             }
  529.  
  530.             /* Negative, the opposite */
  531.  
  532.             else
  533.  
  534.             {
  535.                 sicount = 256 - sicount;
  536.                 bcopy(cptr, dptr, sicount);
  537.                 cptr += sicount;
  538.                 dptr += sicount;
  539.             }
  540.             }
  541.         }
  542.  
  543.         (*func)(FLI_BRUN, i, data, 0,0, 0,0, wwidth,wheight);
  544.         break;
  545.         }
  546.  
  547.     }
  548.     
  549.     /* Send a FLI_SYNC after each frame to get accurate timing */
  550.     (*func)(FLI_SYNC, i, (unsigned char *)0, 0,0, 0,0, 0,0);
  551.     }
  552.  
  553.     /* Only free the buffer if we allocated something */
  554.     if (!buf)
  555.     free(buf);
  556.  
  557. }
  558.